﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.Helper;
using BusinessLogic.Controller;
using DataEntity;
using JScripter;
using BusinessLogic;
using System.Data;
public partial class AjaxPages_Property_AddPropertyStep2 : AjaxPage
{

    private Guid PropertyID
    {
        get
        {
            if (Request.Params["pid"] != null || AjaxState.ContainsKey("pid"))
            {
                if (!AjaxState.ContainsKey("pid"))
                {
                    AjaxState["pid"] = Request.Params["pid"];

                }
                return new Guid(AjaxState["pid"]);
            }
            else
            {
                return Guid.Empty;
            }

        }
    }

    private int PropertyTypeID
    {
        get
        {
            if (PropertyID != Guid.Empty)
            {
                var data = new PropertyController().GetbyPropertyID(PropertyID);
                if (data.Count > 0)
                {
                    return data[0].PropertyTypeID;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDropDowns();
        BindFeatures();
    }

    private void BindUserData()
    {
        var userdata = new LoginUserController().GetbyLoginID(new LoginHelper().LoginID);
        if (userdata.Count > 0)
        {
            lblowner.InnerText = userdata[0].FirstName + " " + userdata[0].LastName;
            lblEmail.InnerText = userdata[0].Username;

        }
        var contactdata = new ContactDetailController().GetbyLoginID(new LoginHelper().LoginID);
        if (contactdata.Count > 0)
        {
            if (contactdata[0].Mobile != null)
            {
                txtPhonenumber.Visible = false;
                lblphonenumber.InnerText = contactdata[0].Mobile;
            }
        }
    }
    public void BindDropDowns()
    {
        PropertyHelper.BindBalcony(ddBalcony);
        PropertyHelper.BindFacing(ddFacing);
        PropertyHelper.BindPropertyonFloor(ddPropertyonFloor);
        PropertyHelper.BindTotalFloorsinconstruction(ddTotalFloorsinconstruction);
        PropertyHelper.BindTypeofFlooring(ddTypeofFlooring);
        PropertyHelper.BindFurnishing(ddFurnishing);

        divbalcony.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddBalcony.ClientID);
        divfacing.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddFacing.ClientID);
        divfloor.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddPropertyonFloor.ClientID);
        divtotalfloor.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddTotalFloorsinconstruction.ClientID);
        divfurning.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddFurnishing.ClientID);
    }

    public void BindFeatures()
    {
        var data = new FeatureController().Get();
        listFeatures.DataSource = data;
        listFeatures.DataBind();
    }

    protected void Done_Click(object sender, AjaxControl.AjaxEventArg e)
    {
        PropertyAditionalFeature propertyAditionalFeature = new PropertyAditionalFeature();

        string anualDuesPayable = HtmlHelper.ControlValue(txtAnnualDuesPayable.ClientID);
        if (anualDuesPayable != null && anualDuesPayable != "0")
            propertyAditionalFeature.AnualDuesPayable = Convert.ToDecimal(anualDuesPayable);

        //propertyAditionalFeature.AditionalRoom

        propertyAditionalFeature.Balcony = HtmlHelper.ControlValue(ddBalcony.ClientID);


        string Balcony = HtmlHelper.ControlValue(ddBalcony.ClientID);
        if (Balcony != "" && Balcony != "0")
            propertyAditionalFeature.Balcony = Balcony;

        string BookingAmount = HtmlHelper.ControlValue(txtBookingAmount.ClientID);
        if (BookingAmount != null && BookingAmount != "0")
            propertyAditionalFeature.BookingAmount = Convert.ToDecimal(BookingAmount);


        string Facing = HtmlHelper.ControlValue(ddFacing.ClientID);
        if (Facing != "" && Facing != "0")
            propertyAditionalFeature.Facing = Facing;

        string Flooring = HtmlHelper.ControlValue(ddTypeofFlooring.ClientID);
        if (Flooring != "" && Flooring != "0")
            propertyAditionalFeature.Flooring = Flooring;

        string Furnishing = HtmlHelper.ControlValue(ddFurnishing.ClientID);
        if (Furnishing != "" && Furnishing != "0")
            propertyAditionalFeature.Furnishing = Furnishing;

        string MaintenanceCharge = HtmlHelper.ControlValue(txtMaintenanceCharges.ClientID);
        if (MaintenanceCharge != null && MaintenanceCharge != "0")
            propertyAditionalFeature.MaintenanceCharge = Convert.ToDecimal(MaintenanceCharge);

        string maintenancePerText = HtmlHelper.ControlValue(ddMaintencePer.ClientID);
        if (maintenancePerText != "" && maintenancePerText != "0")
            propertyAditionalFeature.MaintenancePerText = maintenancePerText;

        propertyAditionalFeature.ModifiedDate = DateTime.Now;

        string pricePerUnitArea = HtmlHelper.ControlValue(txtPricePerUnitArea.ClientID);
        if (pricePerUnitArea != null && pricePerUnitArea != "0")
            propertyAditionalFeature.PricePerUnitArea = Convert.ToDecimal(pricePerUnitArea);

        propertyAditionalFeature.PropertyAditionalFeatureID = Guid.NewGuid();

        string rentalIncome = HtmlHelper.ControlValue(txtRentalIncome.ClientID);
        if (rentalIncome != null && rentalIncome != "0")
            propertyAditionalFeature.RentalIncome = Convert.ToDecimal(rentalIncome);

        string totalFloor = HtmlHelper.ControlValue(ddTotalFloorsinconstruction.ClientID);
        if (totalFloor != "" && totalFloor != "0")
            propertyAditionalFeature.TotalFloor = totalFloor;

        string features = Request.Params["Features"];
        List<Guid> featureId = new List<Guid>();
        if (features!=null && features != ""  )
        {

            string[] feature = features.Split(',');
            for (int i = 0; i < feature.Length; i++)
            {
                featureId.Add(new Guid(feature[i]));
            }
        }

        ProximityLandmark proximityLandmark = new ProximityLandmark();
        proximityLandmark.AdditionalComment = HtmlHelper.ControlValue(txtAdditionalComments.ClientID);


        string airport = HtmlHelper.ControlValue(txtAirport.ClientID);
        if (airport != null && airport != "")
            proximityLandmark.Airport = Convert.ToDecimal(airport);


        string ATM = HtmlHelper.ControlValue(txtATM.ClientID);
        if (ATM != null && ATM != "")
            proximityLandmark.ATM = Convert.ToDecimal(ATM);


        string cityCenter = HtmlHelper.ControlValue(txtCityCenter.ClientID);
        if (cityCenter != null && cityCenter != "")
            proximityLandmark.CityCenter = Convert.ToDecimal(cityCenter);

        string hospital = HtmlHelper.ControlValue(txtHospital.ClientID);
        if (hospital != null && hospital != "")
            proximityLandmark.Hospital = Convert.ToDecimal(hospital);

        proximityLandmark.KeyLandmark = HtmlHelper.ControlValue(txtKeyLandmarks.ClientID);
        proximityLandmark.ModifiedDate = DateTime.Now;
        proximityLandmark.ProximityLandmarkID = Guid.NewGuid();

        string railwayStation = HtmlHelper.ControlValue(txtRailwayStation.ClientID);
        if (railwayStation != null && railwayStation != "")
            proximityLandmark.RailwayStation = Convert.ToDecimal(railwayStation);

        string school = HtmlHelper.ControlValue(txtSchool.ClientID);
        if (school != null && school != "")
            proximityLandmark.School = Convert.ToDecimal(school);

        if (txtPhonenumber.Visible)
        {
            new ContactDetailController().Add(new LoginHelper().LoginID, HtmlHelper.ControlValue(txtPhonenumber.ClientID));
        }

        PropertyHelper.UpdatePropertywithAdditionalData(PropertyID, propertyAditionalFeature, featureId, proximityLandmark);
    }
}